package com.pandora.framework.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.pandora.common.db.DBManager;
import com.pandora.common.util.StringUtils;
import com.pandora.framework.constant.ServletConstants;
import com.pandora.framework.util.DBConfig;
import com.pandora.framework.util.LogUtils;
import com.pandora.framework.util.PropertiesUtils;
import com.pandora.framework.util.ServiceConfigUtils;

/**
 * 系统初始化
 * 
 * @author JingChenglong 2017/12/15 20:11
 *
 */
public class InitServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void init(ServletConfig conf) throws ServletException {

		ServletConstants.SERVLETCONTEXTREALPATH = conf.getServletContext().getRealPath("");// 加载系统路径
		ServletConstants.ENVIRONMENT = StringUtils.nullToStrTrim(conf.getInitParameter("environment"));// 加载运行环境

		/* 加载配置文件 */
		PropertiesUtils.init();
		/* 加载日志线程 */
		LogUtils.init();
		/* 加载接口服务 */
		ServiceConfigUtils.init();
		/* 加载数据源配置 */
		DBConfig.init();

		LogUtils.logInfo(
				PropertiesUtils.getConfig("SYSTEMNAME") + " " + PropertiesUtils.getConfig("VERSION") + " startup");
	}

	@Override
	public void destroy() {
		ServiceConfigUtils.destory();
		DBManager.closeDataSource();
		PropertiesUtils.cleanCache();
		LogUtils.logInfo(
				PropertiesUtils.getConfig("SYSTEMNAME") + " " + PropertiesUtils.getConfig("VERSION") + " shutdown");
	}
}